package com.gexin.service.memberAcitivty;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.BigDecimalUtils;
import com.gexin.model.activity.MD5Util;
import com.gexin.model.activity.MemberBillSpeedIntegral;
import com.gexin.model.activity.MemberInfo;
import com.gexin.model.activity.PayInfo;
import com.gexin.model.enums.MemberInfoEnum;
import com.gexin.model.weixin.WeixinUser;

import java.math.BigDecimal;

public class MemberInfoService {


    public ApiResponse getMemberInfo(String userId){
        MemberInfo memberInfo = MemberInfo.dao.getMemberInfoByUserId(userId);
        if(memberInfo==null){
            memberInfo = new MemberInfo();
            memberInfo.setMemberInfo(System.currentTimeMillis()/1000+"",userId,0,null);
            memberInfo.save();
        }
        return new ApiResponse(true,"返回成功",memberInfo);
    }

    /**
     * 获取用户积分
     * @param authUser
     * @return
     */
    public ApiResponse getSpeedIntegral(WeixinUser authUser) {
        String id = authUser.getId()+"";
        MemberBillSpeedIntegral memberBillSpeedIntegral = MemberBillSpeedIntegral.dao.getMemberBillSpeedIntegralByUserId(id);
        if(memberBillSpeedIntegral==null){
            memberBillSpeedIntegral = new MemberBillSpeedIntegral();
            memberBillSpeedIntegral.setUserIdAndSpeedIntegralAndCoinAndBillCouponAndForecastAndRemainAndReverseAndDailyIncrement
                    (id,BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO,
                    BigDecimal.ZERO);
            memberBillSpeedIntegral.save();
        }
        return new ApiResponse(true,"返回成功",memberBillSpeedIntegral);
    }


    /**
     * 转账时校验个人信息
     *
     * @param authUserId
     * @param presentId
     * @param coin
     * @return
     */
    public ApiResponse checkAuthtication(String authUserId, String presentId, BigDecimal coin,BigDecimal billOfShop, int type) {
        WeixinUser weixinUser = WeixinUser.dao.getWeixinUser(presentId);
        if (weixinUser == null) {
            return new ApiResponse(false, "用户不存在!");
        }
        if (presentId == null) {
            return new ApiResponse(false, "赠送人id为空,请重试");
        }
        if (authUserId.equals(presentId)) {
            return new ApiResponse(false, "自己不能给本人赠送佣金");
        }
        if (type == 1) {
            if (coin == null) {
                return new ApiResponse(false, "请确认赠送的佣金数量不为空");
            }
            if (BigDecimalUtils.lessEqual(coin, BigDecimal.ZERO)) {
                return new ApiResponse(false, "请确认输入的值不小于且不等于0");
            }
        } else if (type == 2 && BigDecimalUtils.lessThan(billOfShop, BigDecimal.ZERO)) {
            if (billOfShop == null) {
                return new ApiResponse(false, "请确认赠送的提货券数量不为空");
            }
            if (BigDecimalUtils.lessEqual(billOfShop, BigDecimal.ZERO)) {
                return new ApiResponse(false, "请确认输入的值不小于且不等于0");
            }
        }

//      TODO 需要填充memberinfo用户信息
//      判断当前用户是否是会员()
        MemberInfo memberInfoByUser = MemberInfo.dao.getMemberInfoByUserId(authUserId);
        int level = 0;
        if (memberInfoByUser == null) {
            return new ApiResponse(false, "本账户不能转账");
        }
        if (memberInfoByUser.getLevel() == MemberInfoEnum.FIRST_LEVEL.getCode()) {
            return new ApiResponse(false, "请先确定您是否是会员身份,游客不能转账");
        }

//      被转账人id是否存在
        MemberInfo presentIdMemberInfoByUser = MemberInfo.dao.getMemberInfoByUserId(presentId);
        if (presentIdMemberInfoByUser == null) {
            return new ApiResponse(false, "请检测输入id是否正确");
        }
//      转账人身份
        Integer presentLelve = presentIdMemberInfoByUser.getLevel();
        if (presentId == null ||presentLelve==null|| presentLelve == 0||presentLelve == 1) {
            return new ApiResponse(false, "收到转账的用户只能是会员,请先确认转账身份");
        }
        return new ApiResponse(true, "返回成功");
    }




    public void setPayInfo(String password,String phone){

        PayInfo payInfo = new PayInfo();
        String passwordMd5 = MD5Util.encodeMD5Hex(password);
        payInfo.setPhone(phone);
        payInfo.setPayPassword(passwordMd5);
        payInfo.save();
    }

}
